import React from 'react';
import {Link} from 'react-router';
import { Form, Input, Row, Col, Button, DatePicker, Select, Icon,  Table, Card,Steps } from 'antd';
import classNames from 'classnames';
import {connect} from 'react-redux'
import SearchInput from '../../components/SearchInput'
import CompanyCard from './companyCard'
import SubmitCard from './submitCard'
import ProfileCard from './profileCard'
import { studentProfile, profileUpdate, companyQuery, companyUpdate, orderApply } from '../../actions'

const FormItem = Form.Item;
const Option = Select.Option;
const ButtonGroup = Button.Group;
const InputGroup = Input.Group;
const Step = Steps.Step;

class OrderApply extends React.Component {

  constructor(props){
    super(props);
    this.state = {
      step:0,
      loading:false
    };
  }

  componentWillMount () {
    console.log('OrderApply.componentWillMount')
    //发起学生个人信息查询
    this.props.dispatch(studentProfile.request()());
    //发起公司信息查询
    this.props.dispatch(companyQuery.request()());
  }

  nextStep(values) {
    if (this.state.step == 2) {
      //提交申请
      this.props.dispatch(orderApply.request()({...values, type:"apply"}));
    } else {
      this.setState({step: this.state.step + 1}, ()=>{
        if (this.state.step === 1) {
          this.props.dispatch(profileUpdate()(values));
        } else if (this.state.step === 2) {
          this.props.dispatch(companyUpdate()(values));
        }
      });
    }
  }

  lastStep(values) {
    this.setState({step: this.state.step - 1}, ()=>{
      if (this.state.step === 0) {
        this.props.dispatch(companyUpdate()(values));
      }
    });
  }

  render() {
    let { profile, loading, company,result } = this.props;
    let content;
    if (this.state.step == 0) {
      content = <Card style={{marginTop:24}} title="学生信息"><ProfileCard profile={profile} loading={loading} onNextClick={this.nextStep.bind(this)} onLastClick={this.lastStep.bind(this)}/></Card>;
    } else if (this.state.step == 1) {
      content = <Card style={{marginTop:24}} title="用人单位信息"><CompanyCard company={company} onNextClick={this.nextStep.bind(this)} onLastClick={this.lastStep.bind(this)}/></Card>;
    } else {
      content = <SubmitCard {...this.props} profile={profile} result={result} company={company} onNextClick={this.nextStep.bind(this)} onLastClick={this.lastStep.bind(this)}/>;
    }
    return (
      <div style={{minHeight:560}}>
        <Steps current={this.state.step}>
          <Step title="第一步" description="完善个人信息"></Step>
          <Step title="第二步" description="完善用人单位信息"></Step>
          <Step title="第三步" description="提交"></Step>
        </Steps>
        {content}
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    profile: state.student,
    loading:state.student.loading,
    company:state.company,
    result:state.order.success,
  };
}

export default connect(mapStateToProps)(OrderApply)
